package com.niit.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.niit.pojo.Admin;
import com.niit.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.niit.utils.ByteArrayToBase64Adapter.GSON;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @RequestMapping ("/login")
    public String login(Admin admin) {
        JsonObject jsonObject = new JsonObject();
        List<Admin> admins = adminService.queryAdmin(admin);
        if(admins != null  && admins.size() ==1) {
            jsonObject.addProperty("code", 1);
            jsonObject.add("success", GSON.toJsonTree(admins.get(0)).getAsJsonObject());
            System.out.println(admins.get(0).toString());
        }else {
            jsonObject.addProperty("code", 0);
        }
        return jsonObject.toString();
    }

    @RequestMapping("/add")
    public String addAdmin(Admin admin) {
        JsonObject jsonObject = new JsonObject();
        List<Admin> admins = adminService.queryAdmin(new Admin(admin.getAdminName()));
        if(admins == null || admins.isEmpty()) {
            int i = adminService.addAdmin(admin);
            jsonObject.addProperty("code",i);
        }else{
            jsonObject.addProperty("code",0);
        }
        return jsonObject.toString();
    }

    @RequestMapping("/delete")
    public String deleteAdmin(Admin admin) {
        JsonObject jsonObject = new JsonObject();
        List<Admin> admins = null;
        if (admin.getAdminId() == null) {
            admins = adminService.queryAdmin(new Admin(admin.getAdminName()));
        }
        if (admins != null) {
            int i = adminService.deleteAdmin(admins.get(0).getAdminId());
            jsonObject.addProperty("code", i);
        } else {
            jsonObject.addProperty("code", 0);
        }
        return jsonObject.toString();
    }

    @RequestMapping("/update")
    public String updateAdmin(Admin admin){
        JsonObject jsonObject = new JsonObject();
        int i = adminService.updateAdmin(admin);
        jsonObject.addProperty("code", i);
        if(i == 1){
            admin = adminService.queryAdmin(admin.getAdminId());
            jsonObject.add("admin", new Gson().toJsonTree(admin).getAsJsonObject());
        }
        return jsonObject.toString();
    }

    @RequestMapping("/query")
    public String queryAdmin(Admin admin){
        JsonObject jsonObject = new JsonObject();
        List<Admin> admins = adminService.queryAdmin(admin);
        if(admins != null && !admins.isEmpty()) {
            jsonObject.addProperty("code", 1);
            jsonObject.add("success", GSON.toJsonTree(admins).getAsJsonArray());
        }else {
            jsonObject.addProperty("code", 0);
        }
        return jsonObject.toString();
    }

}
